import axios from 'axios'
import { useUserStore } from '@/stores/userStore.js'

const instance = axios.create({
    baseURL: '/api',
    timeout: 1000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
})

// 添加请求拦截器
instance.interceptors.request.use(
    config => {
        const userStore = useUserStore()
        if (!config.noAuth && userStore.isLoggedIn && userStore.token) {
            config.headers.Authorization = `Bearer ${userStore.token}`
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

export default instance